﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Services;

namespace SharingCenter.Web.Mod.Contact
{
    using SharingCenter.Web.Classes;
    using SharingCenter.Web.Classes.Requests;
    using SharingCenter.Web.Classes.Contact;
    using SharingCenter.Web.Classes.SiteEvents;
    using System.Xml.Linq;

    /// <summary>
    /// Summary description for ContactWebService
    /// </summary>
    [WebService(Namespace="")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    [ScriptService]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class ContactWebService : System.Web.Services.WebService
    {

        [WebMethod]
        [ScriptMethod]
        public void ApproveContactRequest(string requestId)
        {
            if (Context.User.Identity.IsAuthenticated)
            {
                bool mutualContact = true;
                Guid rid = new Guid(requestId);
                RequestOperations.ChangeRequestStatus(rid, SharingCenter.Web.Classes.enums.RequestStatus.Approved, DateTime.Now);
                Sc_SharedRequest req = ContactOperations.ApproveContact(rid, mutualContact, true);
                Guid requestee = new Guid(req.Context.Element("RequesteeUserId").Value);
                XElement cxt = new XElement("Event",
                    new XElement("MinorParty", requestee),
                    new XElement("MajorParty", req.RequestorUserId)
                    );
                SiteEventOperations.AddEvent("Contact", "ContactApproved", "Contact", req.RequestorUserId, cxt, true, null);
                cxt = new XElement("Event",
                    new XElement("MinorParty", req.RequestorUserId),
                    new XElement("MajorParty", requestee)
                    );
                SiteEventOperations.AddEvent("Contact", "ContactApproved", "Contact", requestee, cxt, true, null);
            }
        }

        [WebMethod]
        [ScriptMethod]
        public void RejectContactRequest(string requestId)
        {
            if (Context.User.Identity.IsAuthenticated)
            {
                Guid rid = new Guid(requestId);
                RequestOperations.ChangeRequestStatus(rid, SharingCenter.Web.Classes.enums.RequestStatus.Rejected, DateTime.Now);
            }
        }

        [WebMethod]
        [ScriptMethod]
        public bool SendContactRequest(string requestorUserId, string requesteeUserId, string comment)
        {
            if (Context.User.Identity.IsAuthenticated)
            {
                Guid requestor = new Guid(requestorUserId);
                Guid requestee = new Guid(requesteeUserId);
                try
                {
                    ContactOperations.AddContactRequest(requestor, requestee, comment);
                    return true;
                }
                catch { return false; }
            }
            else
            {
                return false;
            }
        }
    }
}
